<script setup lang="ts">
import { userLoginApi, adminLoginApi } from '@/api/modules/login'
// import { Login } from '@/api/types/modules/login'
import { GlobalStore } from '@/store'
import { initDynamicRouter } from '@/router/modules/dynamicRouter'
import { KeepAliveStore } from '@/store/modules/keepAlive'
import { getTimeState } from '@/utils'
import { HOME_URL } from '@/config/config'

const globalStore = GlobalStore()
const keepAlive = KeepAliveStore()
const router = useRouter()

const login = async (type: string) => {
  const api = type === 'user' ? userLoginApi : adminLoginApi
  const { data } = await api()
  // 设置 token
  globalStore.setToken(data.token)
  // 2.添加动态路由
  await initDynamicRouter()

  // 清空 keepAlive 保留的数据
  keepAlive.setKeepAliveName()

  // 4.跳转到首页
  router.push(HOME_URL)
  ElNotification({
    title: getTimeState(),
    message: '欢迎登录',
    type: 'success',
    duration: 3000,
  })
}
</script>

<template>
  <div style="text-align: center">
    <h2 class="mt40 mb40">这是登录页面</h2>
    <el-button type="primary" @click="login('admin')">admin 登录</el-button>
    <el-button type="warning" @click="login('user')">user 登录</el-button>
  </div>
</template>
